package com.yk.silence.rxmvp.net.retrofit;

import android.util.Log;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.orhanobut.logger.Logger;
import com.yk.silence.rxmvp.base.BaseView;
import com.yk.silence.rxmvp.net.api.APIServer;
import com.yk.silence.rxmvp.net.file.ProgressResponseBody;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtils {

    private static RetrofitUtils sInstance;
    private Retrofit retrofit;
    private OkHttpClient client;
    private final String BASE_URL = "/";
    private static final int READ_TIME_OUT = 20;
    private static final int WRITE_TIME_OUT = 20;
    private static final int CONNECT_TIME_OUT = 20;
    private static final String TAG = RetrofitUtils.class.getSimpleName();
    private APIServer apiServer;


    private RetrofitUtils() {

    }

    /**
     * 单例模式
     *
     * @return
     */
    public static RetrofitUtils getInstance() {
        if (sInstance == null) {
            synchronized (RetrofitUtils.class) {
                if (sInstance == null) {
                    sInstance = new RetrofitUtils();
                }
            }
        }
        return sInstance;
    }

    /**
     * okhttp
     * @return
     */
    private OkHttpClient okHttpClient() {
        /**
         * 日志拦截器
         */
        HttpLoggingInterceptor httpLoggingInterceptor =
                new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Logger.e(TAG, message.toString());
                    }
                });
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .addInterceptor(httpLoggingInterceptor)
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .build();
        return client;
    }

    /**
     * 请求访问quest
     * response拦截器
     */
    private Interceptor interceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            long startTime = System.currentTimeMillis();
            Response response = chain.proceed(chain.request());
            long endTime = System.currentTimeMillis();
            long duration = endTime - startTime;
            MediaType mediaType = response.body().contentType();
            String content = response.body().string();
            Log.e(TAG, "----------Request Start----------------");
            Log.e(TAG, "| " + request.toString() + request.headers().toString());
            Log.e(TAG, "| Response:" + content);
            Log.e(TAG, "----------Request End:" + duration + "毫秒----------");
            return response.newBuilder()
                    .body(ResponseBody.create(mediaType, content))
                    .build();
        }
    };

    /**
     * 获取retrofit
     * @return
     */
    public Retrofit retrofit(){
        retrofit=new Retrofit.Builder()
                .client(client)
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        apiServer=retrofit.create(APIServer.class);
        return retrofit;
    }

    /**
     * 获取APIServer
     * @return
     */
    public APIServer getApiServer() {
        return apiServer;
    }
}
